package com.spongebob.controller.hqz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spongebob.pojo.*;
import com.spongebob.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 历史会话控制器
 */
@Controller
public class SessionHistoryController {

    @Autowired
    private SessionMessageService sessionMessageService;
    @Autowired
    private SessionService sessionService;
    @Autowired
    private CustomerInfoService customerInfoService;
    @Autowired
    private TagsService tagsService;
    @Autowired
    private VisitorInfoService visitorInfoService;

    /**
     * 初始会话历史页面
     *
     * @param model       // model
     * @param httpSession // 测试客服 id 为 54328，需要登录时传入客服 id
     * @return "index"
     */
    @RequestMapping("/chatHistory")
    public String chatHistory(Model model, HttpSession httpSession) {
        CustomerService customerService = (CustomerService) httpSession.getAttribute("user");
        // 创建 list
        List<Session> list = new ArrayList<>();

        // 查出所有sessionId
        List<Integer> sessionIds = sessionMessageService.selectAllSessionById(customerService.getCustomerserviceid());


        // 循环所有sessionId
        for (Integer sessionId : sessionIds) {
            Session session = sessionService.selectSessionById(sessionId);
            list.add(session);
        }


        // 会话列表
        model.addAttribute("ssss", list);
        // 设置status让index页面渲染
        model.addAttribute("status", 3);
        return "index";
    }

    /**
     * 查询会话历史
     *
     * @param id    // 传入点击 session 时传入 session 的 id
     * @param model // model
     * @return "forward:/chatHistory"
     */
    @RequestMapping("/chatHistory/historyAndData")
    public String findHistoryAndData(@RequestParam("id") Integer id, Model model, HttpSession httpSession) {

        CustomerService customerService = (CustomerService) httpSession.getAttribute("user");

        System.out.println("sessionId " + id);
        // 查询对应sessionMessage
        QueryWrapper<SessionMessage> qw = new QueryWrapper<>();
        qw.eq("customerServiceId", customerService.getCustomerserviceid()); // customerServiceId
        qw.eq("sessionId", id);
        List<SessionMessage> list = sessionMessageService.list(qw);

        // 根据id查session
        Session ss = sessionService.selectSessionById(id);
        System.out.println("session " + ss);


        // 查询访客信息
        QueryWrapper<VisitorInfo> vqw = new QueryWrapper<>();
        vqw.eq("visitorName", ss.getVisitorname());
        VisitorInfo visitor = visitorInfoService.getOne(vqw);
        System.out.println("visitor " + visitor);

        // 查询客户信息
        QueryWrapper<CustomerInfo> cqw = new QueryWrapper<>();
        cqw.eq("customerName", ss.getVisitorname());
        cqw.eq("customerServiceId", customerService.getCustomerserviceid()); // customerServiceId
        CustomerInfo customerInfo = customerInfoService.getOne(cqw);
        System.out.println("customer " + customerInfo);

        // 查询tag
        if (customerInfo != null) {
            Tags tag = tagsService.getById(customerInfo.getTagsid());
            // tag
            model.addAttribute("tag", tag);

            // 客户
            model.addAttribute("customer", customerInfo);
        }
        if (visitor != null) {
            // 访客
            model.addAttribute("visitor", visitor);
        }
        // session
        model.addAttribute("sd", ss);


        // session history
        model.addAttribute("history", list);
        return "forward:/chatHistory";
    }
}
